<!doctype html>
<html>
<head>
    <title>OMC企业云运维云平台-服务器控制台</title>
    <link rel="stylesheet" href="${wwwroot}/resources/xterm/src/xterm.css" />
    <link rel="stylesheet" href="${wwwroot}/resources/xterm/addons/fullscreen/fullscreen.css" />
    <link rel="stylesheet" href="${wwwroot}/resources/xterm/style.css" />
    <script src="${wwwroot}/resources/xterm/src/xterm.js" ></script>
    <script src="${wwwroot}/resources/xterm/addons/attach/attach.js" ></script>
    <script src="${wwwroot}/resources/xterm/addons/fit/fit.js" ></script>
    <script src="${wwwroot}/resources/xterm/addons/fullscreen/fullscreen.js" ></script>
</head>
<body>
<h1>
    IP：${(server.host)!}
</h1>
<div id="terminal-container"></div>
<#--<div>-->
    <#--<h2>Options</h2>-->
    <#--<label><input type="checkbox" id="option-cursor-blink"> cursorBlink</label>-->
<#--</div>-->
<#--<script src="${wwwroot}/resources/xterm/main.js" defer ></script>-->
</body>
</html>
<script>
    var term,
            protocol,
            socketURL,
            socket;

    var terminalContainer = document.getElementById('terminal-container');
    createTerminal();

    function createTerminal() {
        while (terminalContainer.children.length) {
            terminalContainer.removeChild(terminalContainer.children[0]);
        }
        term = new Terminal({
//            cursorBlink: optionElements.cursorBlink.checked
            cursorBlink: true
        });
        protocol = (location.protocol === 'https:') ? 'wss://' : 'ws://';
        socketURL = "ws://${(serverroot)!}/admin/console.ws?serverId=${serverId!}&t="+ new Date().getTime();
        socket = new WebSocket(socketURL);

        term.open(terminalContainer);
        term.fit();

        socket.onopen = runRealTerminal;
        socket.onmessage = console;
        socket.onclose = runFakeTerminal;
        socket.onerror = runFakeTerminal;
    }

    var cmd="";

    function runRealTerminal() {
        term.attach(socket);
        term._initialized = false;

        term.on('key', function (key, ev) {
//            row = term.y + term.ybase;
            var printable = (
                    !ev.altKey && !ev.altGraphKey && !ev.ctrlKey && !ev.metaKey
            );
            if (ev.keyCode == 13) {//回车减一行，否则重复
                term.y--;
            }
            else {
                term.write(key);
            }
        });
    }

    function console()
    {
        term.writeln('');
    }

    function runFakeTerminal() {

//        var shellprompt = '$ ';
//
//        term.prompt = function () {
//            term.write(shellprompt);
//        };
//
        term.writeln('');
        term.writeln('服务器连接失败！');
//        term.writeln('This is a local terminal emulation, without a real terminal in the back-end.');
//        term.writeln('Type some keys and commands to play around.');
//        term.writeln('');
        term.prompt();
//
//        term.on('key', function (key, ev) {
//            alert("key:"+key);
//            var printable = (
//                    !ev.altKey && !ev.altGraphKey && !ev.ctrlKey && !ev.metaKey
//            );
//
//            if (ev.keyCode == 13) {
//                term.prompt();
//            } else if (ev.keyCode == 8) {
//                /*
//               * Do not delete the prompt
//               */
//                if (term.x > 2) {
//                }
//            } else if (printable) {
//
//                term.write(key);
//            }
//        });
//
//        term.on('paste', function (data, ev) {
//            term.write(data);
//        });
//
//        term.on('message', function (data, ev) {
//            term.write(data);
//        });
    }

</script>
